<template>
  <view class="login_contanier">
    <uni-icons class="login_icon" type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <button class="login_btn" type="primary" @click="getuserinfo">一键登录</button>
    <text class="login_tip">登录后尽享更多权益</text>
  </view>
</template>

<script>
  import {mapMutations,mapState} from 'vuex'
  export default {
    name:"mylogin",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState('user',['redirectInfo'])
    },
    methods:{
      ...mapMutations('user',['updateuserInfo','updateToken','updateRedirectInfo']),
      getuserinfo(){
        uni.getUserProfile({
          desc:'请求授权',
          success: (res) => {
            this.updateuserInfo(res.userInfo)
            this.getToken(res)
          },
          fail: (res) => {
            return uni.$showMsg('您已取消授权')
          }
        })
      },
     async getToken(info){
        const [err,res] =await uni.login().catch(err=>err)
        if(err||!res.errMsg==='login:ok')return uni.$showMsg('登录失败')
        const query={
          code:res.code,
          encryptedData:info.encryptedData,
          rawData:info.rawData,
          signature:info.signature,
          iv:info.iv
        }
        // 接口失效,直接手写token
        // const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
        // console.log(loginResult);
        // if(success.meta!==200)return uni.$showMsg('登录失败')
        const token="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
        this.updateToken(token)
        this.navigaBack()
      },
      navigaBack(){
       if(this.redirectInfo&&this.redirectInfo.openType==='switchTab'){
         uni.switchTab({
           url:this.redirectInfo.from,
           complete: () => {
             this.updateRedirectInfo(null)
           }
         })
       }else {
         uni.navigateTo({
           url:this.redirectInfo.from,
           complete: () => {
             this.updateRedirectInfo(null)
           }
         })
       }
      }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: white;
  }
.login_contanier{
  height: 750rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8f8f8;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  
  &::after {
    content: ' ';
    display: block;
    width: 100%;
    height: 40px;
    border-radius: 100%;
    transform: translateY(50%);
    position: absolute;
    left: 0;
    bottom: 0;
    background-color: white;
  }
  .login_btn{
    background-color: #C00000;
    width: 90%;
    border-radius: 100rpx;
    margin: 15px 0;
  }
  .login_tip {
    font-size: 12px;
    color: gray;
  }
}
</style>